package com.grts.mall.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.grts.mall.common.CommonResult;
import com.grts.mall.common.ResultForLayuiTable;
import com.grts.mall.pojo.Category;
import com.grts.mall.pojo.Image;
import com.grts.mall.pojo.Product;
import com.grts.mall.pojo.vo.ProductAddVo;
import com.grts.mall.pojo.vo.ProductSearchConditionVo;
import com.grts.mall.service.CategoryService;
import com.grts.mall.service.ImageService;
import com.grts.mall.service.ProductService;

@Controller
public class ProductController {

	@Autowired
	private ProductService productService;

	@Autowired
	private CategoryService cagoryService;
	
	@Autowired
	private ImageService imageService;

	@RequestMapping("/product/manage")
	public String list() {
		return "product/manage";
	}

	@RequestMapping("/product/list")
	@ResponseBody
	public ResultForLayuiTable listJson(ProductSearchConditionVo vo) {
		
		long count = this.productService.countByExample(vo);
		List<Product> data = this.productService.findPageList(vo);
		return new ResultForLayuiTable(0, "OK", data, count);
	}

	@RequestMapping("/product/del")
	@ResponseBody
	public CommonResult del(Long pid) {
		this.productService.del(pid);
		return CommonResult.success();
	}

	@RequestMapping("/product/add")
	public String add(Model model) {
		List<Category> cats = this.cagoryService.findall();
		model.addAttribute("cats", cats);
		return "product/add";
	}

	@RequestMapping("/product/save")
	@ResponseBody
	public CommonResult save(ProductAddVo product){
		
		this.productService.save(product);
		return CommonResult.success();
	}

	@RequestMapping("/batch/del")
	@ResponseBody
	public CommonResult batchDel(@RequestParam("pid[]") List<Long> pid) {
		this.productService.batchDel(pid);
		return CommonResult.success();
	}

	@RequestMapping("/product/edit")
	public String edit(Long pid, Model model) {
		System.out.println(pid);
		List<Category> cats = cagoryService.findall();
		Product product = this.productService.findById(pid);
		List<Image> images = this.imageService.findByPidImages(pid);
		model.addAttribute("pro", product);
		model.addAttribute("cats", cats);
		model.addAttribute("imgs", images);
		return "product/edit";
	}
	
	@RequestMapping("/product/cover")
	@ResponseBody
	public CommonResult cover(@RequestPart("file") MultipartFile multipartFile) throws IllegalStateException, IOException {
		String path = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/upload";
		File uploadDir = new File(path);
		if (!uploadDir.exists()) {
			uploadDir.mkdir();
		}
		
		//随机名字
		String fileName = UUID.randomUUID().toString();
		//在html中用img的src来访问虚拟路径
		String imgSrc = "/upload/"+fileName;
		//存文件
		File file = new File(path, fileName);
		multipartFile.transferTo(file);
		HashMap<String, String> data = new HashMap<>();
		data.put("src", imgSrc);
		data.put("title", fileName);
		return CommonResult.success("上传成功", data);
	}
	
	@RequestMapping("/product/updata")
	@ResponseBody
	public CommonResult updata(ProductAddVo product) {
//		System.out.println(product.getImgs());
		this.imageService.del(product);
		this.productService.updata(product);
		return CommonResult.success();
	}
	@RequestMapping("/product/onsale")
	@ResponseBody
	public CommonResult onsale(Long pid,Boolean onSale) {
		Product pro = this.productService.findById(pid);
		pro.setOnSale(onSale);
		this.productService.updata(pro);
		return CommonResult.success();
	}

}
